package com.lcxw.hxzbadmin.controller.livecontroller;
import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbadmin.entity.pojo.LiveType;
import com.lcxw.hxzbadmin.service.baseservice.LiveTypeService;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbcommon.common.StringUtil;

@RestController
@CrossOrigin
@RequestMapping("admin/liveType")
public class LiveTypeController {

    private Logger logger = LoggerFactory.getLogger(LiveTypeController.class);

    @Resource
    LiveTypeService liveTypeService;

    @PostMapping("/add")
    public Response addLiveType(@RequestBody JSONObject requestParams) {
        String adminName = requestParams.getString("adminName");

        String type = requestParams.getString("type");
        int level = Integer.parseInt(requestParams.getString("level"));
        String superior = requestParams.getString("superior");
        logger.info("admin {} add a liveType.", adminName);

        if (2 == level && StringUtil.isEmpty(superior)) {
            return Response.common(logger, "二级直播类型必须携带上级的类型名称。" + type, ErrorCode.INVALID_REQ,
                "level 2 type must carry superior.", null);
        }

        LiveType liveType = new LiveType();
        liveType.setType(type);
        liveType.setLevel(level);
        liveType.setSuperior(superior);

        //生成添加时间 yyyy-MM-dd HH:mm:ss
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date addTime = new Date();
        liveType.setAddTime(sdf.format(addTime));

        try {
            liveTypeService.addLiveType(liveType);
        } catch (Exception e) {
            return Response.dbFail(logger, "add liveType");
        }

        return Response
            .common(logger, "add liveType success: " + liveType, 0, "success.", null);

    }

    @PostMapping("/delete")
    public Response deleteLiveType(@RequestBody JSONObject requestParams) {
        String adminName = requestParams.getString("adminName");

        int id = requestParams.getInteger("id");
        logger.info("admin {} delete liveType id={}.", adminName, id);
        if (0 == id) {
            return Response
                .common(logger, "id must not be null.", ErrorCode.PARAM_ERROR, "id must not be null.",
                    null);
        }

        try {
            liveTypeService.deleteLiveType(id);
        } catch (Exception e) {
            return Response.dbFail(logger, "delete liveType ");
        }

        return Response.common(logger, "delete liveType success.", 0, "success", null);

    }

    @PostMapping("/getAll")
    public Response getAllLiveType(@RequestBody JSONObject requestParams) {
        String adminName = requestParams.getString("adminName");
        logger.info("admin {} get all liveType.", adminName);

        Map<String, Object> data = new LinkedHashMap<>();
        try {
            List<LiveType> liveTypes = liveTypeService.getAllLiveTypes();
            int count = liveTypes.size();
            data.put("count", count);
            data.put("liveTypes", liveTypes);
        } catch (Exception e) {
            return Response.dbFail(logger, "get all liveType");
        }

        return Response.common(logger, "get all liveTypes success.", 0, "success", data);
    }

    @PostMapping("/mod")
    public Response modLiveType(@RequestBody JSONObject requestParams){
        String adminName = requestParams.getString("adminName");
        int id = requestParams.getInteger("id");
        String type = requestParams.getString("type");
        int level = Integer.parseInt(requestParams.getString("level"));
        String superior = requestParams.getString("superior");

        logger.info("admin {} modify liveType {}.",adminName,id);

        LiveType liveType;

        try {
            liveType = liveTypeService.getLiveTypeById(id);
            if (null == liveType){
                return Response.common(logger,"liveType not exist ",ErrorCode.INVALID_PARAM,"liveType not exist",null);
            }
            liveType.setType(type);
            liveType.setLevel(level);
            liveType.setSuperior(superior);

            liveTypeService.updateLiveType(liveType);

        } catch (Exception e) {
            return Response.dbFail(logger,"operate liveType");
        }

        return Response.common(logger,"update liveType success.",0,"success",null);
    }
}
